package com.caine.androidtask.ls09;

/**
 @author : CaineZhu
 @date : 2013-01-15 13:22:46
 @desc  : for command  *#*#935#*#* *#*#66#*#* *#*#38#*#* *#*#945#*#*

 */




import com.caine.androidtask.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

//import android.os.SystemProperties; 
@SuppressLint({ "WorldWriteableFiles", "WorldReadableFiles" })
@SuppressWarnings("unused")
public class Eng extends Activity {

	private Button btn_shake = null;
	private Button btn_audio = null;
	private TextView softwareinfo = null;
	private TextView hardwareinfo = null;
	private Button btn_backlight = null;
	private Context context = null;
	PowerManager pm = null;

	// private static String DISPALY_OUT_HARDWARE_VERSION =
	// SystemProperties.get("ro.spreadtone.hwversion","data is none!");
	// private static String DISPALY_OUT_SOFTWARE_VERSION =
	// SystemProperties.get("ro.spreadtone.version","data is none!");
	// private static String DISPALY_INTERNAL_HARDWARE_VERSION =
	// SystemProperties.get("ro.spreadtone.hwversion","data is none!");
	// private static String DISPALY_INTERNAL_SOFTWARE_VERSION =
	// SystemProperties.get("ro.spreadtone.internalswversion","data is none!");

	
	private static String DISPALY_OUT_HARDWARE_VERSION = " : DISPALY_OUT_HARDWARE_VERSION";
	
	private static String DISPALY_OUT_SOFTWARE_VERSION = " : DISPALY_OUT_SOFTWARE_VERSION";
	private static String DISPALY_INTERNAL_HARDWARE_VERSION = " : DISPALY_INTERNAL_HARDWARE_VERSION";
	private static String DISPALY_INTERNAL_SOFTWARE_VERSION = " : DISPALY_INTERNAL_SOFTWARE_VERSION";
	private static final String PATH = "data/data/com.caine.eng/shared_prefs/CAINEZHU_CONFIG.xml";
	//private static final String PATH = "data/default.prop";
	private WakeLock mWakeLock;
	boolean FLAGS = false;
	String STATUS_STRING = "";
	Editor editor = null;
	SharedPreferences sp  = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.caine_ls09); 
		init();
		
	}

	void init() {
	
		MListener mListener = new MListener();
		btn_shake = (Button) findViewById(R.id.eng_btn_shake);
		btn_audio = (Button) findViewById(R.id.eng_btn_audio);
		softwareinfo = (TextView) findViewById(R.id.softwareinfo);
		hardwareinfo = (TextView) findViewById(R.id.hardwareinfo);
		softwareinfo.setText(DISPALY_INTERNAL_SOFTWARE_VERSION);
		hardwareinfo.setText(DISPALY_INTERNAL_HARDWARE_VERSION);
		btn_backlight = (Button) findViewById(R.id.eng_btn_backlight);
		//msg.setTextColor(Color.RED);
        
		btn_shake.setOnClickListener(mListener);
		btn_audio.setOnClickListener(mListener);
		btn_backlight.setOnClickListener(mListener);
		
	}

	class MListener implements View.OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			System.out.println("FLAGS is :"+FLAGS);
			switch (v.getId()) {
			
			case R.id.eng_btn_shake:
				if (!FLAGS) {
					
					 FLAGS = true;
					 editor = sp.edit();
					 editor.putString("VISBRATOR", "1");
				     editor.commit();
					startService(new Intent(Eng.this, VibrateServer.class));					
				} else {
					 editor = sp.edit();
					 editor.putString("VISBRATOR", "0");
				     editor.commit();
				     if(mWakeLock != null){
				     mWakeLock.release();
				     }
				     mWakeLock = null;
				     
					stopService(new Intent(Eng.this, VibrateServer.class));
					
					FLAGS = false;
				}
				break;
			case R.id.eng_btn_audio:
				//ReadConfigUtils.readConfig(PATH);
				Intent i = new Intent(Intent.ACTION_MAIN);
				i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				ComponentName comp = new ComponentName("com.android.settings",
						"com.android.settings.SoundSettings");
				i.setComponent(comp);
				startActivity(i);
				break;
			case R.id.eng_btn_backlight:
				if(mWakeLock == null){
				mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK,
						"vibrator engtest");
				mWakeLock.acquire();
				Toast.makeText(Eng.this, "FULL_WAKE_LOCK", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(Eng.this, "FULL_WAKE_LOCK CLOSED", Toast.LENGTH_SHORT).show();
					mWakeLock.release();
					mWakeLock = null;
				}
			default:
			}
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		context = getContextByPackageName(this.getPackageName());   
		System.out.println("this.getPackageName() is :"+this.getPackageName());
		sp = context.getSharedPreferences("CAINEZHU_CONFIG", MODE_WORLD_WRITEABLE+MODE_WORLD_READABLE);
		if(sp.getString("VISBRATOR", "0").equals("0")){
		FLAGS = false;
		}else if(sp.getString("VISBRATOR", "1").equals("1")){
			FLAGS = true;
		}else{
			FLAGS = false;
			System.out.println("VISBRATOR is not found!!!");
		}
		if (mWakeLock == null) {
			pm = (PowerManager) this.getApplication()
					.getSystemService(Context.POWER_SERVICE);
			
		}

	}


	
	public Context getContextByPackageName(String packageName){
		Context ct = null;
		try {
			ct = createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ct;
	}
  public void showMsg(String msg){
	  Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  }
}
